using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Text;

namespace Ex01.Controllers {
  public class ContentTypeController : Controller {
    public IActionResult Index() {
      return View();
    }
    public string Fruits(){
      string[] fruits = { "Apple", "Ciku", "Durian", "Rambutan" };
      StringBuilder html = new StringBuilder("<select>");
      foreach (var fruit in fruits) {
        html.Append($"<option>{fruit}</option>");
      }
      html.Append("</select>");
      return html.ToString();
    }
    public ContentResult Fruits2() { 
      string[] fruits = { "Apple", "Ciku", "Durian", "Rambutan" };
      StringBuilder html = new StringBuilder("<select>");
      foreach (var fruit in fruits) {
        html.Append($"<option>{fruit}</option>");
      }
      html.Append("</select>");
      //return base.Content(html.ToString(), "text/html");
      return
         new ContentResult {
           ContentType = "text/html",
           StatusCode = (int)HttpStatusCode.OK,
           Content = html.ToString()
         };
    }

    public IActionResult SVG() {
      var green = "#00FF00";
      var yellow = "#ffff00";
      var data =@$"
         <svg width='100' height='100' xmlns='http://www.w3.org/2000/svg'>
           <circle cx='50' cy='50' r='40' stroke='{green}' 
                stroke-width='4' fill='{yellow}' />
         </svg>";
      return Content(data, "image/svg+xml");
    }

    public JsonResult JSON() {//JSON=JavaScript Object Notation
      var person = new {
        Name = "Yong Tau Foo",
        Age = 18,
        Married = false
      };

      return Json(person);
    }

    public ContentResult XML() {
      var xml = @"
              <Students>
                <Student>
                  <Name>Alibaba</Name>
                  <Age>19</Age>
                  <CGPA>2.4</CGPA>
                </Student>
                <Student>
                  <Name>Abumama</Name>
                  <Age>20</Age>
                  <CGPA>3.1</CGPA>
                </Student>
              </Students>  
            ";

      return new ContentResult {
        Content = xml,
        ContentType = "application/xml",
        StatusCode = 200
      };
    }

  }
}
